<?php
/* Modovi - includes/database/class.php
   author: Modovi-Team (http://modovi.de/ | info@modovi.de)
   license: GNU General Public License v3 (http://www.gnu.org/licenses/gpl-3.0.html) */

class Database {
        var $connected = false;
        var $selectedDB = false;
// Connect with a database cluster
// Parameter: Hostname, Username, Passwort
        public static function ConnectToDB()
        {
                global $connected;

                if (!$connected)
                {
                        require('../includes/config.php');
                        mysql_connect( $dbhost, $dbusername, $dbpassword );
                        echo "<!-- Connected to DB! -->";
                        $connected = true;
                }
        }

// Select a database
// Parameter: Datenbank
        public static function SelectDB()
        {
                global $connected, $selectedDB;

                if ($connected && !$selectedDB)
                {
                        require('../includes/config.php');
                        mysql_select_db( $dbname );
                        $selectedDB = true;
                }
        }

// Select a simple value from a table
// Parameter: tablename, column
        public static function SelectSimple( $table, $column )
        {
                global $connected, $selectedDB;

                if ($connected && $selectedDB)
                {
                        $simplequery = mysql_query("SELECT $column FROM $table");
                        $result[] = array();
                        while ($qrow = mysql_fetch_row($simplequery))
                        {
                                $result[] = $qrow;
                        }
                        return $result;
                }
                return false;
        }

// Select a simple value from a table (with WHERE)
// Parameter: tablename, column, where
        public static function Select( $table, $column, $where)
        {
                global $connected, $selectedDB;

                if ($connected && $selectedDB)
                {
                        $query = mysql_query("SELECT $column FROM $table WHERE $where");
                        echo '<!-- SELECT * FROM '.$table.' WHERE '.$where."-->"; // Testing...
						$result[] = array();
                        while ($qrow = mysql_fetch_row($query))
                        {
                                $result[] = $qrow;
                        }
						if (count($result) <= 1)
						{
							return $result[0];
						}
						return $result;
                }
                return false;
        }

// Updates a column (or a single value with optional $where) in a table
// Parameter: tablename, column, newvalue, [where]
        public static function Update( $table, $column, $newvalue, $where = '' )
        {
                global $connected, $selectedDB;

                if ($connected && $selectedDB)
                {
                        return mysql_query("UPDATE $table SET $column = '$newvalue'" . (($where) ? ' WHERE ' . $where : ''));
                }
                return false;
        }

// Insert values into optional columns in a table
        public static function Insert( $table, $values, $columns = '' )
        {
                global $connected, $selectedDB;

                if ($connected && $selectedDB)
                {
                        return mysql_query("INSERT INTO $table" . (($columns) ? " ($columns)" : '') . " VALUES ($values)");
                }
                return false;
        }

        public static function Delete( $table, $where )
        {
                global $connected, $selectedDB;

                if ($connected && $selectedDB)
                {
                        return mysql_query("DELTE FROM $table WHERE $where");
                }
                return false;
        }
}
?>
